package router

import (
	"partyus_app/controllers"
	"partyus_app/middleware"

	"github.com/gin-gonic/gin"
)

// SetRouter 设置路由
func SetRouter() *gin.Engine {
	r := gin.Default()
	r.Use(middleware.CORSMiddleware())
	r.Static("/public", "./public")

	// 公开的注册和登录路由，表示不需要鉴权
	noauth := r.Group("/api/noauth")
	{
		noauth.POST("/login", controllers.Login)
		noauth.POST("/register", controllers.Register)
		noauth.POST("/request-code", controllers.RequestCode)
		noauth.POST("/browse", controllers.Browse)
		noauth.GET("/get-post-detail", controllers.GetPostDetail)
		noauth.GET("/user-get-by-Email", controllers.GetUserByEmail)
		noauth.GET("/user-get", controllers.GetUser)
		noauth.GET("/get-self-post", controllers.GetSelfPost)

		noauth.POST("/verify-info", controllers.VerifyInfo)
		noauth.GET("/validate-token", controllers.ValidateToken)
	}

	// 保护的路由，需要鉴权
	protected := r.Group("/api/auth")
	protected.Use(middleware.VerifyJWT())
	{
		// 在这里定义需要保护的路由
		protected.POST("/publish-post", controllers.PublishPost)
		protected.POST("/upload-file", controllers.UploadFile)
		protected.POST("/upload-avatar", controllers.UploadAvatar)

		//GetUserIDByToken
		protected.GET("/get-user-by-token", controllers.GetUserByToken)

		protected.POST("/request-by-id", controllers.RequestCodeByID)
		protected.PUT("/user-update", controllers.UpdateUser)
		//UpdatePassword
		protected.PUT("/password-update", controllers.UpdatePassword)

		//更新帖子内容接口
		protected.PUT("/update-post", controllers.UpdatePost)
		//GetDeletedPosts用户查看自己删除的帖子
		protected.POST("/get-deleted-post", controllers.GetDeletedPosts)
		//删除帖子
		protected.DELETE("/delete-post", controllers.DeletePost) // 新增删除帖子路由

		// 管理员管理账号的路由，额外通过区团工委鉴权中间件
		adminRoutes := protected.Group("/admin")
		adminRoutes.Use(middleware.CheckDistrictRole()) // 使用区团工委鉴权中间件
		{
			adminRoutes.GET("/get-unapproved-list", controllers.GetApprovedList)
			adminRoutes.PUT("/approve-action", controllers.ApproveAction)

			adminRoutes.POST("/user-add", controllers.CreateUser)
			adminRoutes.DELETE("/user-del", controllers.DeleteUser)
			adminRoutes.PUT("/user-update", controllers.UpdateUser)

			//获取用户列表GetUserList
			adminRoutes.GET("/get-user-list", controllers.GetUserList)
			//ResetUserPassword
			adminRoutes.PUT("/reset-password", controllers.ResetUserPassword)

		}

	}

	return r
}
